<?php
session_start();
//include ('header2.php');
include('databaseconnection.php');

  $email=$_SESSION['email'];
  $dd=gmdate(DATE_ATOM,time());
$d=explode("T",$dd);
  $term=$_POST['term'];
 
  $domain=$_POST['domain']; 
   $newdomain=$_POST['newdomain'];
  $language=$_POST['language']; 
  $newlanguage=$_POST['newlanguage'];
  $suggestion=$_POST['newsuggestion']; 
  //="Admin"; 
  $suggested_date=$d[0];//"2012-02-21";
  $votes=0; 
  $comments="";
  $status="Pending Review"; 
  
  
  if($term=="" || $suggestion=="")
{
header("Location:Add_Suggestion.php?msgsu=Term and Suggestion are not null........");
}
else if($domain=="" && $newdomain=="")
{
header("Location:Add_Suggestion.php?msgsu=Domain are not null..... please select one of the domain or add the new domain of your term...");
}
else if($domain!="" && $newdomain!="")
{
header("Location:Add_Suggestion.php?msgsu=Please select one of the domain Or add the new domain of your term...");
}
else if($language=="" && $newlanguage=="")
{
header("Location:addterm.php?msg=Language are not null..... please select one of the language or add the language...");
}
else if($language!="" && $newlanguage!="")
{
header("Location:addterm.php?msg=please select one of the language Or add the language...");
}
 else
 {
 
 $resulte=mysql_query("SELECT * FROM suggestions WHERE term='$term' and suggestion='$suggestion'");
if(!$resulte) echo "invalid";

if(mysql_num_rows($resulte) == 1) 
{
header("Location:Add_Suggestion.php?msgsu=Suggestion is already exist.....");
}
$resultet=mysql_query("SELECT * FROM terms WHERE term='$term' and accepted_meaning='$suggestion'");
if(!$resultet) echo "invalid";

else if(mysql_num_rows($resultet) == 1)
{
header("Location:Add_Suggestion.php?msgsu=Your Suggestion is already exist in Glossary.....");
}

 else
 {
 
 $result=mysql_query("SELECT * FROM user WHERE email='$email'");
if(!$result) echo "invalid";
while($row=mysql_fetch_array($result))
{

$suggested_by=$row['first_name'];


if($language=="")
  {
  $resultsl=mysql_query("SELECT * FROM language WHERE language='$newlanguage' ");
  if (mysql_num_rows($resultsl) == 1) 
		{
		  $ilanguage=$newlanguage;
		}
		else
		{
  $ilanguage=$newlanguage;
  $query="INSERT INTO language (language) values ('$newlanguage')";
  $rez1=mysql_query($query);
  if(!$rez1) {echo "INvalid".mysql_error();}
  }
  }
  if($newlanguage=="")
  {
  $ilanguage=$language;
  }








  if($domain=="")
  {
  $resultsd=mysql_query("SELECT * FROM domain WHERE domain='$newdomain' ");
  if (mysql_num_rows($resultsd) == 1) 
		{
		  $idomain=$newdomain;
		}
		else
		{
  $idomain=$newdomain;
  $query="INSERT INTO domain (domain) values ('$newdomain')";
  $rez1=mysql_query($query);
  if(!$rez1) {echo "INvalid".mysql_error();}
  }
  }
  if($newdomain=="")
  {
  $idomain=$domain;
  }

$query="INSERT INTO suggestions (term, domain, language, suggestion, suggested_by, suggested_date, status,votes,comments) VALUES ('$term', '$idomain', '$ilanguage', '$suggestion', '$suggested_by', '$suggested_date','$status','$votes','$comments')";
$rez=mysql_query($query);
  if(!$rez) {echo "INvalid".mysql_error();}
  header("Location:Add_Suggestion.php?msgsu=Your Suggestion is added.......");
  /*echo   "<table width='960px' height='82' border='1' class='table' align='left'>";
   echo "<tr><td>Your Suggestion is added, to <a href='Suggestion.php'>View</a></td></tr>";
   echo "</table>";*/
   }
   }
   }
  ?>